Overview
enrich_fno_data.py is a Phase 4c pipeline script that enriches the master stock database with Futures & Options (F&O) data. It fetches current lot sizes and expiry calendar information from Dhan’s API and maps them to stocks flagged as F&O-eligible in the master ISIN map.
Pipeline Position: Phase 4c - Final enrichment step before data exportCritical Function: Adds trading-essential F&O metadata required for options and futures analysis
Purpose
This script:- Identifies F&O-eligible stocks from
master_isin_map.json - Fetches real-time lot sizes from Dhan
- Retrieves next expiry dates from the F&O calendar
- Enriches
all_stocks_fundamental_analysis.jsonwith F&O metadata
Input Files
Master stock database containing all fundamental and technical data
ISIN mapping file with
FnoFlag field (1 = F&O eligible, 0 = not eligible)Output Files
Updated master file with three new fields added to each stock record:
F&O: “Yes” or “No”Lot Size: Integer lot size or “N/A”Next Expiry: Date string (YYYY-MM-DD) or “N/A”
Processing Logic
1. Build ID Extraction
Dynamically fetches the Next.jsbuildId from Dhan’s lot size page:
2. Lot Size Fetching
Retrieves current month lot sizes for all F&O instruments:3. Expiry Date Fetching
Retrieves the next upcoming expiry date for each F&O symbol:4. Data Enrichment
Maps F&O data to stocks based on the FnoFlag:Fields Added
Indicates F&O eligibility
"Yes"- Stock is F&O eligible"No"- Stock is not F&O eligible
Current month lot size for F&O contracts
- Integer value (e.g.,
500,1000) for F&O stocks "N/A"for non-F&O stocks
Next upcoming expiry date
- Date in YYYY-MM-DD format (e.g.,
"2026-03-26") for F&O stocks "N/A"for non-F&O stocks
Usage Example
Error Handling
- Missing master_isin_map.json: Script continues but no stocks are marked as F&O eligible
- Build ID fetch failure: Returns empty lot size and expiry maps
- API timeout: Individual fetch functions fail gracefully, returning empty dictionaries
- Symbol mismatch: Stocks not found in Dhan data receive “N/A” values
Data Source
- F&O Eligibility:
master_isin_map.json(FnoFlag field) - Lot Sizes: Dhan NSE F&O Lot Size API
- Expiry Dates: Dhan F&O Expiry Calendar API
Related Scripts
- bulk_market_analyzer.py - Creates the master stock database
- process_market_breadth.py - Uses F&O flags for analytics